<?php
namespace api\modules\merchant\controllers\banner;

use backend\widgets\provinces\Provinces;
use Yii;
use api\modules\merchant\models\ResetPasswordForm;
use yii\web\NotFoundHttpException;
use api\controllers\OnAuthController;
use common\enums\StatusEnum;
use common\helpers\ResultDataHelper;


/**
 * Banner接口
 *
 * Class InfoController
 * @package api\modules\merchant\controllers\banner
 * @property \yii\db\ActiveRecord $modelClass
 * @author tjj
 */
class InfoController extends OnAuthController
{
    public $modelClass = 'common\models\banner\Banner';

    /**
     * 删除
     * @param $id
     * @return bool|mixed
     * @throws NotFoundHttpException
     */
    public function actionDelete($id)
    {
        $model = $this->modelClass::findOne(['id'=>$id]);
        if($model){
            $this->checkStore($model['store_id']);
        }else return ResultDataHelper::api(422, '请求的数据不存在');

        return parent::actionDelete($id); // TODO: Change the autogenerated stub
    }

    /**
     * 我的店铺列表
     * @return array|\yii\data\ActiveDataProvider|\yii\db\ActiveRecord[]
     */
    public function actionIndex()
    {
        $request = Yii::$app->request;
        $store_id = $request->get('store_id')??0;

        if(!$store_id)
            return ResultDataHelper::api(422, '店铺id不能为空');

        $this->checkStore($store_id);

        $list = $this->modelClass::find()
            ->select([])
            ->where(['store_id'=>$store_id,'status' => StatusEnum::ENABLED])
            ->asArray()
            ->all();

        return $list;
    }

    /**
     * @param $id
     * @return \yii\db\ActiveRecord
     * @throws NotFoundHttpException
     */
    public function actionView($id)
    {
        $model = $this->modelClass::findOne(['id'=>$id]);
        if($model){
            $this->checkStore($model['store_id']);
        }else return ResultDataHelper::api(422, '请求的数据不存在');

        return parent::actionView($id); // TODO: Change the autogenerated stub
    }

    /**
     * 创建店铺
     * @return mixed|\yii\db\ActiveRecord|null
     * @throws NotFoundHttpException
     */
    public function actionCreate()
    {
        $res = parent::actionCreate(); // TODO: Change the autogenerated stub

        $model = $this->modelClass::findOne(['id'=>$res->id]);

        if (!$model)
        {
            throw new NotFoundHttpException('请求的数据不存在或您的权限不足.');
        }

        return $model;
    }

    /**
     * 修改信息
     * @param $id
     * @return array|mixed|\yii\db\ActiveRecord|null
     * @throws NotFoundHttpException
     */
    public function actionUpdate($id)
    {
        $model = $this->modelClass::findOne(['id'=>$id]);
        if($model){
            $this->checkStore($model['store_id']);
        }else return ResultDataHelper::api(422, '请求的数据不存在');

        parent::actionUpdate($id); // TODO: Change the autogenerated stub

        $model = $this->modelClass::find()
            ->where(['id' => $id, 'status' => StatusEnum::ENABLED])
//            ->select(['id','type', 'username', 'nickname', 'realname', 'head_portrait', 'gender', 'qq', 'email', 'birthday', 'user_money', 'accumulate_money','frozen_money','user_integral', 'visit_count','home_phone','mobile','role','last_time','last_ip','provinces','city','area','status', 'created_at','updated_at'])
            ->asArray()
            ->one();

        if (!$model)
        {
            throw new NotFoundHttpException('请求的数据不存在或您的权限不足.');
        }

        return $model;
    }

    /**
     * 权限验证
     *
     * @param string $action 当前的方法
     * @param null $model 当前的模型类
     * @param array $params $_GET变量
     * @throws \yii\web\BadRequestHttpException
     */
    public function checkAccess($action, $model = null, $params = [])
    {
        // 方法名称
//        if (in_array($action, ['delete', 'index']))
//        {
//            throw new \yii\web\BadRequestHttpException('权限不足');
//        }
    }
}
